﻿using Common.AspNetCore;
using Common.RunModels.File;
using File.Api.Repository;
using File.Data.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace File.Api.Service
{
    public class FileService : CommonService
    {
        private readonly FileRepository fileRepository;

        public FileService(IServiceProvider provider, FileRepository file) : base(provider)
        {
            this.fileRepository = file;
        }

        public async Task<List<FileInfoModel>> GetFileInfoAsync(List<string> ids)
        {
            var files = await this.fileRepository.GetFilesByIdsAsync(ids);
            if (files == null)
            {
                return null;
            }

            List<FileInfoModel> ret = new List<FileInfoModel>();
            foreach (var r in files)
            {
                var info = new FileInfoModel()
                {
                    FileId = r.id,
                    FileName = r.file_name,
                    FilePath = r.file_path,
                    FileType = r.file_type,
                    CreateTime = r.create_time,
                    UserName = r.user_name,
                    FileLength = r.file_length
                };
                ret.Add(info);
            }

            return ret;
        }

        public async Task<List<FileGroupInfoModel>> GetFileGroupInfoAsync(List<string> ids)
        {
            var orderModel = await this.fileRepository.GetFileGroupOrderByIdsModelAsync(ids);
            var filegroups = orderModel?.FileGroups; 
            if (filegroups == null || !filegroups.Any())
            {
                return null;
            }

            var r1 = orderModel.FileGroupDetails;
            List<string> fileIds = new List<string>();
            fileIds.AddRange(filegroups.Where(t => !string.IsNullOrEmpty(t.file_id)).Select(t => t.file_id));
            if (r1 != null && r1.Any())
            {
                fileIds.AddRange(r1.Select(t => t.file_id));
            }

            IEnumerable<t_file> files = null;
            if (fileIds.Any())
            {
                files = await this.fileRepository.GetFilesByIdsAsync(fileIds);
            }

            List<FileGroupInfoModel> ret = new List<FileGroupInfoModel>();
            foreach (var r in filegroups)
            {
                FileGroupInfoModel info = new FileGroupInfoModel()
                {
                    CreateTime = r.create_time,
                    FileGroupId = r.id,
                    FileId = r.file_id,
                    FileName = r.file_name,
                    FileType = r.file_type,
                    UserName = r.user_name,
                    FileLength = r.file_length
                };
                var f = files != null && !string.IsNullOrEmpty(r.file_id) ? files.FirstOrDefault(t => t.id == r.file_id) : null;
                if (f != null)
                {
                    info.FilePath = f.file_path;
                }

                r1.ForEach(t1 =>
                {
                    var f1 = files != null && !string.IsNullOrEmpty(t1.file_id) ? files.FirstOrDefault(t => t.id == t1.file_id) : null;
                    var d = new FileGroupDetailInfoModel()
                    {
                        CreateTime = t1.create_time,
                        FileId = t1.file_id,
                        Index = t1.index,
                        UserName = r.user_name,
                    };
                    if (f1 != null)
                    {
                        d.FileName = f1.file_name;
                        d.FileType = f1.file_type;
                        d.FilePath = f1.file_path;
                        d.FileLength = f1.file_length;
                    }

                    info.Details.Add(d);
                });
                ret.Add(info);
            }

            return ret;
        }
    }
}
